﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Tomahawk.Runtime.Logic.UI;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Timeline
{
    public sealed class UIControlRotationTrack : BaseTrack
    {
        [XmlElement("Rotation")]
        public string rotation = "";

        private Curve curveRotation = null;

        public UIControlRotationTrack() { }

        public UIControlRotationTrack(string name) : base(name) { }

        protected override bool OnLoad(GraphicsDevice graphics, ContentManager contentManager)
        {
            curveRotation = LoadCurve(contentManager, rotation);

            return base.OnLoad(graphics, contentManager);
        }

        public override void SortKeys() { }

        public override bool Update(float time)
        {
            if (curveRotation == null)
            {
                return false;
            }

            XUIPlaceableControl control = this.target as XUIPlaceableControl;
            if (control != null)
            {
                Vector2 origin = new Vector2();
                origin.X = control.Position.OriginX;
                origin.Y = control.Position.OriginY;

                float rotation = curveRotation.Evaluate(time);

                control.SetRotation(rotation, origin);
                return true;
            }

            return false;
        }
    }
}
